<template>
  <div>
    <el-dialog
      title="选择产品"
      :visible.sync="dialogVisible"
      append-to-body
      width="80%"
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="产品名称" prop="productName">
          <el-input
            v-model="queryParams.productName"
            placeholder="请输入产品名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="产品厂家" prop="productFactory">
          <el-input
            v-model="queryParams.productFactory"
            placeholder="请输入产品厂家"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="市场价" prop="marketPrice">
          <el-input
            v-model="queryParams.marketPrice"
            placeholder="请输入市场价"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="出货价" prop="shippingPrice">
          <el-input
            v-model="queryParams.shippingPrice"
            placeholder="请输入出货价"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="productList">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column
          label="产品厂家"
          align="center"
          prop="productFactory"
        />
        <el-table-column label="市场价" align="center" prop="marketPrice" />
        <el-table-column label="出货价" align="center" prop="shippingPrice" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="choose(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-dialog>

    <el-dialog
      title="折扣申请"
      :visible.sync="visible"
      width="80%"
      append-to-body
    >
      <el-row>
        <el-form ref="form" :model="form" label-width="110px" :rules="rules">
          <el-col :span="24">
            <el-form-item label="是否申请折扣" prop="isDiscount">
              <el-radio-group v-model="form.isDiscount">
                <el-radio label="0">申请折扣</el-radio>
                <el-radio label="1">不申请折扣</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="产品名称"
              v-if="form.isDiscount === '0'"
              prop="productName"
            >
              <el-input v-model="product.productName" readonly> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="出货价"
              v-if="form.isDiscount === '0'"
              prop="shippingPrice"
            >
              <el-input v-model="product.shippingPrice" readonly>
                <template slot="append">{{
                  digitUppercase(product.shippingPrice)
                }}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="折扣后金额"
              v-if="form.isDiscount === '0'"
              prop="discountPrice"
            >
              <el-input type="number" v-model="form.discountPrice">
                <template slot="append">{{
                  digitUppercase(form.discountPrice)
                }}</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="折扣率"
              v-if="form.isDiscount === '0'"
              prop="discountRate"
            >
              <el-input v-model="form.discountRate" readonly>
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item
              label="申请备注"
              v-if="form.isDiscount === '0'"
              prop="remark"
            >
              <el-input
                v-model="form.remark"
                type="textarea"
                :autosize="{ minRows: 4 }"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct } from "@/api/crm/product";

export default {
  name: "Product",
  data() {
    return {
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品列表表格数据
      productList: [],
      form: {
        isDiscount: "1",
      },
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        productFactory: null,
        marketPrice: null,
        shippingPrice: null,
        productFunction: null,
        productFile: null,
        productScheme: null,
        status: null,
      },
      dialogVisible: false,
      visible: false,
      product: {},
      rules: {
        isDiscount: [
          {
            required: true,
            message: "申请折扣状态不能为空",
            trigger: "change",
          },
        ],
        discountPrice: [
          { required: true, message: "折扣后价格不能为空", trigger: "blur" },
        ],
        discountRate: [
          { required: true, message: "折扣率不能为空", trigger: "change" },
        ],
        remark: [
          { required: true, message: "申请理由不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    "form.discountPrice": {
      handler() {
        this.form.discountRate = (
          ((parseFloat(this.form.discountPrice) || 0) /
            (parseFloat(this.product.shippingPrice) || 1)) *
          100
        ).toFixed(2);
      },
      deep: true,
    },
  },
  methods: {
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if(this.form.isDiscount === '1'){
            this.form.discountPrice = null
            this.form.discountRate = null
            this.form.remark = null
          }

          this.product.remark = this.form.remark
          this.form = { ...this.form, ...this.product };
          this.$emit("setInfo", this.form);
          this.visible = false;
          this.dialogVisible = false;
        }
      });
    },
    cancel() {
      this.visible = false;
      this.form = {};
    },
    open() {
      this.dialogVisible = true;
      this.getList();
    },
    choose(row) {
      this.visible = true;
      this.product = row;
    },
    /** 查询产品列表列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleClose(done) {
      this.dialogVisible = false;
    },
  },
};
</script>
